import platform

# 从BaseManager继承的QueueManager:
import time

import facade
from xjlibrary.mprocesspoll.process_distributed.BaseTask import BaseTask
from xjlibrary.network.MyIP import get_local_ip, GetLocalIPByPrefix
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class SendTask(BaseTask):
    def __init__(self):
        super().__init__()
        sysstr = platform.system()
        if sysstr == "Windows":
            self.ip = GetLocalIPByPrefix("192.168.")
        else:
            self.ip = get_local_ip("enp2s0")
        self.port = 5000
        self.init_send(self.ip, self.port)


class SelectTask(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)

    def selectDB(self):
        sSql = "select `AccessionNumber`,`docid` from `articlenew` where `stat`=0 limit 100000"
        rows = self.mysqlutils.SelectFromDB(sSql)
        return rows

    def dealsql(self, sql):
        self.mysqlutils.ExeSqlToDB(sql)


if __name__ == "__main__":
    sendtask = SendTask()
    select = SelectTask()
    sendtask.start()
    sendtask.set_task_result_obj()
    while True:
        rows = select.selectDB()
        if rows:
            num = 0
            for row in rows:
                num = num + 1
                sendtask.set_task((row[0], row[1]))
                print(num)
        print("time.sleep")
        time.sleep(100)
